﻿

(function () {
    "use strict";

    F.GameLogic = function (container, addButton, removeButton) {
        var self = this;
        self.container = container;
        self.addButton = addButton;
        self.removeButton = removeButton;
        self.environment = null;
        self.radiocommunication = null;
        self.compressor = null;
        self.clickOnContainer = new F.Delegate();
        self.clickAddDriver = new F.Delegate();
        self.clickRemoveDriver = new F.Delegate();
    };

    var glp = F.GameLogic.prototype;

    glp.start = function(){
        var self = this;

        self.initializeEvents();

        var environment = self.environment = new F.Environment(self.container);
        var radio = self.radiocommunication = new F.RadioCommunications();
        var compressor = self.compressor = new F.Compressor(self.containre, self.environment);
        new F.Background(self.container);
        

        var maxFish = Math.floor(Math.random() * 10) + 3;
        for (var i = 0; i < maxFish; i++)
            new F.Fish(self.container, environment);


        self.initializeStarCreating();

        self.createAqualunger();
        
        GraphicsManager.initialize();

    } 

    glp.initializeStarCreating = function(){
        var self = this;
        self.clickOnContainer.add(self.createStar, self);
    }

    glp.initializeEvents = function(){
        var self = this;
        self.container.onclick = (function(sender){
            return function(e){
                //Вычисляем здесь клик по игровым координатам

                //var rect = self.container.getBoundingClientRect();
                var rect = self.getOffsetRect(self.container);
                var realX = e.pageX - rect.left;
                var realY = e.pageY - rect.top;
                if (!sender.environment.validateRealCoordForStar(realX, realY))
                    return;
                var xy = sender.environment.translateRealToGame(realX, realY);
                sender.clickOnContainer.fire(xy.x, xy.y);
            }
        })(self);

        self.addButton.onclick = (function (sender) {
            return function (e) {
                sender.createAqualunger();
            }
        })(self);

        self.removeButton.onclick = (function (sender) {
            return function (e) {
                sender.removeAqualunger();
            }
        })(self);

    }

    glp.createAqualunger = function () {
        var self = this;
        var startXY = self.environment.getStartPositionAqualunger();
        new F.Aqualunger(self.container, self.environment, self.radiocommunication, startXY.x, startXY.y);
    }

    glp.removeAqualunger = function () {
        var self = this;
        self.environment.removeAqualunger();
    }

    glp.createStar = function(x,y){
        var self = this;
        new F.Star(self.container, self.environment, x, y);
    }

    glp.getOffsetRect = function(elem){
        // (1)
        var box = elem.getBoundingClientRect();

        // (2)
        var body = document.body;
        var docElem = document.documentElement;

        // (3)
        var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop;
        var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft;

        // (4)
        var clientTop = docElem.clientTop || body.clientTop || 0;
        var clientLeft = docElem.clientLeft || body.clientLeft || 0;

        // (5)
        var top = box.top + scrollTop - clientTop;
        var left = box.left + scrollLeft - clientLeft;

        return { top: Math.round(top), left: Math.round(left) };
    }

    glp = null

} ());